package cn.good.yan.m2;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * 监听器 - 监听消息 - 消费者-限流
 * 演示1：需要启动生产者（MyMessageListener）启动该服务、消费者（Test2）模拟发送消息
 * 生产者发送消息，消费者，这边监听会得到消息
 *
 * @author shengren.yan
 * @create 2024-06-10
 */
@Component
@Slf4j
public class MyXLListener1 {

    public static final String EXCHANGE_DIRECT = "exchange.direct.order";
    public static final String ROUTING_KEY = "order";
    public static final String QUEUE_NAME = "queue.order";

    // 消费者监听消息
//    @RabbitListener(queues = QUEUE_NAME)
    public void processMessage2(String dateString, Message message, Channel channel) throws InterruptedException, IOException {
        log.info("消费者接受到消息：" + dateString);
        long tag = message.getMessageProperties().getDeliveryTag();
        // System.out.println(10 / 0);
        // 模拟 超时处理，逻辑的耗时
        TimeUnit.SECONDS.sleep(1);
        channel.basicAck(tag, false);
    }

}
